package com.autologistics.action;

import java.util.List;





import java.util.Map;

import org.apache.poi.hssf.record.UseSelFSRecord;

import com.autologistics.model.User;
import com.autologistics.service.UserService;
import com.autologistics.util.GobalValue;
import com.autologistics.util.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{
	UserService userService;
	List<User> listuser;
	User user = new User();
	private String message;
	private int renId;
	private int renrole;
	private String renname;
	private PageBean pageBean;
	//默认是1
	private int page=1;
	
	

	/**
	 * 列出所有用户
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String listUser(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		listuser=userService.listAll();
		if (listuser==null){
			message="暂无用户";
			return SUCCESS;
		}
		pageBean=userService.queryForOnePage(GobalValue.pageSize, page, listuser.size());
		listuser=pageBean.getUserList();
		return "success";
	}
	/**
	 * 准备添加用户
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preAddUser(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		return SUCCESS;
	}
	/**
	 * 添加用户
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String addUser(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		userService.save(user);
		return this.listUser();
	}
	/**
	 * 准备修改用户信息
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preEditUser(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		user=userService.getById(user.getId());
		return SUCCESS;
	}
	/**
	 * 修改用户信息
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String editUser(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		User u = userService.getById(user.getId());
		user.setPwd(u.getPwd());
		userService.update(user);
		return this.listUser();
	}
	/**
	 * 准备修改密码
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preEditUserPwd(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		return SUCCESS;
	}
	/**
	 * 修改用户密码
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String editUserPwd(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		User u = userService.getById(user.getId());
		u.setPwd(user.getPwd());
		userService.update(u);
		return this.listUser();
	}
	public boolean checkLogin(){
		Map<String, Object> attibutes = ActionContext.getContext().getSession();
		if (attibutes.get("id")==null){
			message="未登陆，请重新登陆";
			return false;
		}
		String s=attibutes.get("id").toString();
		renId=Integer.parseInt(s);
		if (attibutes.get("role")!=null){
			s=attibutes.get("role").toString();
			renrole=Integer.parseInt(s);
		}
		renname=attibutes.get("name").toString();
		return true;
	}
	
	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getRenId() {
		return renId;
	}

	public void setRenId(int renId) {
		this.renId = renId;
	}

	

	public int getRenrole() {
		return renrole;
	}

	public void setRenrole(int renrole) {
		this.renrole = renrole;
	}

	public String getRenname() {
		return renname;
	}

	public void setRenname(String renname) {
		this.renname = renname;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	public List<User> getListuser() {
		return listuser;
	}

	public void setListuser(List<User> listuser) {
		this.listuser = listuser;
	}
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return this.user;
	}
}
